//index.js
//获取应用实例
const app = getApp()
let canvas = require('../../common/js/canvas.js'); //引入canvas 方法
let Axios = require('../../common/js/ajax.js');
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 1, //当前导航
		height: 866,
		top: 0,
		imgHeight: 0,
		hcImg: '',
		shareTk: false,
		canvasTk: false,
		followTk: false,
		store_id: 0,
		goods: [],
		shop: {},
		tips: false,
		isIpx:app.globalData.isIpx?true:false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this;
		this.uid = app.globalData.uid || ''
		this.login = this.selectComponent("#login");
		this.load = this.selectComponent("#loading");
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		this.setData({
			store_id: e.id
		})
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Store/store_detail', { //banner
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id
		}, (res) => {
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					shop: data
				})
			}
		})
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/store_goods', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			type: 0,
			p: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					goods: data
				})
				_this.load.loadIng()
			} else {
				_this.showModal(res)
			}
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '店铺详情'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	handleCloseTap: function() {
		this.setData({
			followTk: false, //取消关注		
			canvasTk: false, //合成图片		
			shareTk: false,
			repTk: false,
			height: 0,
			tips: false
		})
	},
	handleFollowTap: function() { //关注
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var _this = this;
		var follow = _this.data.shop.is_follow;
		if (follow == 1) {
			_this.setData({
				followTk: true
			})
		} else {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: this.data.store_id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.setData({
						'shop.is_follow': 1
					})

				}
			})
		}
	},
	handleCancelFoTap: function(e) { //取消关注
		var _this = this;
		Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
			store_id: this.data.store_id,
			uid: this.uid,
			sign: this.Md5,
			time: this.timesp
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				_this.setData({
					'shop.is_follow': 0,
					followTk: false
				})

			}
		})
	},
	handleShareTap: function(e) { //分享弹窗		
		var _this = this;
		this.setData({
			shareTk: true,
			top: 0
		})
	},
	handleSxTap: function() {
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
	},
	handleCanvasTap: function() {
		var HEIGHT = 570;
		var _this = this;
		var picinit = 0;
		var logoinit = 0;
		var textH = canvas.heightComputed(this.data.shop.description, 560, 28, 37)
		picinit = textH
		textH += canvas.heightComputed(this.data.shop.share_content, 510, 28, 37)
		logoinit = textH
		this.setData({
			height: (textH + HEIGHT),
			imgHeight: (textH + HEIGHT)
		})
		wx.showLoading({
			title: '绘图中···',
		})
		var avatarUrl = '';
		var temp_picture = '';
		var wxEwm = ''
		Axios.post(app.globalData.baseUrl + 'App/PublicCommon/Share/share_img', {
			object_id: _this.data.shop.id,
			type: 1
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				wx.downloadFile({
					url: app.globalData.baseUrl + data,
					success: function(res) {
						wxEwm = res.tempFilePath
						wx.downloadFile({
							url: app.globalData.baseUrl + _this.data.shop.icon,
							success: function(res) {
								console.log(res.tempFilePath)
								avatarUrl = res.tempFilePath
								draws()
							}
						})
					}
				})
			}
		})
		function draws() {
			app.ctx.setFillStyle('white')
			app.ctx.fillRect(0, 0, 650, (textH + HEIGHT) * .552)
			var avatar = {
				src: avatarUrl,
				x: (650 - 88) / 2,
				y: 130,
				width: 88,
				height: 88
			}
			var proImg = {
				src: wxEwm,
				x: (650 - 182) / 2,
				y: 85,
				width: 182,
				height: 182
			}
			canvas.drawImg(proImg)
			canvas.drawAvatar(avatar)
			var author = {
				x: 325,
				y: 310,
				color: '#006e88',
				size: 28,
				align: 'center',
				baseline: 'middle',
				text: _this.data.shop.title,
				bold: true
			}
			canvas.drawText(author)
			var adverter = {
				x: 34,
				y: picinit + 390,
				width: 620,
				height: 33,
				color: '#006e88',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: true,
				text: _this.data.shop.share_content
			}
			canvas.textWrap(adverter)
			var desc = {
				x: 34,
				y: 381,
				width: 580,
				height: 38,
				color: '#3c5069',
				size: 24,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: _this.data.shop.description
			}
			canvas.textWrap(desc)
			var canvasLogo = {
				src: '/img/canvasLogo.png',
				x: (650 - 333) / 2,
				y: 451 + logoinit,
				width: 333,
				height: 39
			}
			canvas.drawImg(canvasLogo)
			app.ctx.draw()
			setTimeout(function() {
				wx.canvasToTempFilePath({
					x: 0,
					y: 0,
					canvasId: 'firstCanvas',
					success: function(res) {
						wx.hideLoading();
						// 设置画布高度							
						_this.setData({
							hcImg: res.tempFilePath,
							canvasTk: true, //合成图片							
							shareTk: false,
							height: 0
						})
					}
				});
			}, 1000)
		}
	},
	handleSaveTap: function(e) { //保存到相册
		let _this = this;
		wx.getImageInfo({
			src: _this.data.hcImg, //
			success: function(res) {
				wx.saveImageToPhotosAlbum({
					filePath: res.path,
					success: function(dres) {
						wx.showToast({
							title: '已保存到相册'
						})
					}
				})
			}
		})
	},
	onShareAppMessage: function() {
		let _this = this;
		this.setData({
			shareTk: false,
		})
		return {
			title: _this.data.shop.share_content, // 转发后 所显示的title
			path: '/pages/shopDetail/shopDetail?id=' + _this.data.store_id, // 相对的路径
			success: function(res) { // 成功后要做的事情
				console.log(res.shareTickets[0])
				// console.log
				wx.getShareInfo({
					shareTicket: res.shareTickets[0],
					success: function(res) {

					},
					fail: function(res) {
						console.log(res)
					},
					complete: function(res) {
						console.log(res)
					}
				})
			},
			fail: function(res) {
				// 分享失败
				console.log(res)
			}
		}
	},
	handlelettertap:function(e){
		var id = e.currentTarget.dataset.id;
		let user = wx.getStorageSync('userInfo') || {}
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		if(id == user.identifier){
			wx.showModal({
				content: '不能和自己聊天',
				showCancel: false,
			})
			return false
		}
		wx.navigateTo({
			url:'/pages/center/chatRoom/chatRoom?id='+id
		})
	}
})
